/**
 *顺序打印-进阶版
 *
 * 作业内容
 * 有三个线程，分别只能打印A，B和C
 * 要求按顺序打印ABC，打印10次
 * 输出示例：
 * ABC
 * ABC
 * ABC
 * ABC
 * ABC
 * ABC
 * ABC
 * ABC
 * ABC
 * ABC
 */

/*
这里是开始不会写 看的题解
public class Test1 {

    // 三个线程 三把锁, 每一把锁各自锁各自的线程.
    private static final Object locker1 = new Object();
    private static final Object locker2 = new Object();
    private static final Object locker3 = new Object();

    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {

                    synchronized (locker1) {
                        locker1.wait(); // 等待唤醒, 然后打印 A
                    }
                    System.out.print("A");
                    synchronized (locker2) {
                        locker2.notify(); // 线程1打印完就唤醒线程2
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t2 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (locker2) {
                        locker2.wait();
                    }
                    System.out.print("B");
                    synchronized (locker3) {
                        locker3.notify(); // 线程2打印完就唤醒线程3
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t3 = new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    synchronized (locker3) {
                        locker3.wait();
                    }
                    System.out.println("C");
                    synchronized (locker1) {
                        locker1.notify(); // 线程3打印完就唤醒线程1, 如此循环10次
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();
        t2.start();
        t3.start();

        Thread.sleep(1000);

        // 从线程 t1 启动
        synchronized (locker1) {
            locker1.notify();
        }
    }
}
 */

// 看题解自己写的
public class Test1 {

    // 三个线程 三把锁
    public static final Object locker1 = new Object();
    public static final Object locker2 = new Object();
    public static final Object locker3 = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    synchronized (locker1) {
                        locker1.wait(); // 等待唤醒, 第一次由main线程唤醒, 其余次都会被线程3唤醒
                    }
                    System.out.print("A");
                    synchronized (locker2) {
                        locker2.notify(); // 唤醒线程2
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    synchronized (locker2) {
                        locker2.wait(); // 等待由线程1唤醒
                    }
                    System.out.print("B");
                    synchronized (locker3) {
                        locker3.notify(); // 唤醒线程2
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread t3 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    synchronized (locker3) {
                        locker3.wait(); // 等待线程2唤醒
                    }
                    System.out.println("C");
                    synchronized (locker1) {
                        locker1.notify(); // 唤醒线程1
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t1.start();
        t2.start();
        t3.start();

        Thread.sleep(500);

        // 从线程1 开始执行
        synchronized (locker1) {
            locker1.notify();
        }
    }
}